package com.study.mike.controller;

import cn.hutool.core.util.ObjectUtil;
import com.study.mike.common.annotation.MyLog;
import com.study.mike.common.enums.MethodTypeEnum;
import com.study.mike.model.dto.QuerySaleOrderProductDTO;
import com.study.mike.model.dto.SaleOrderDTO;
import com.study.mike.model.po.*;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.OrderService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author KangHui
 * @Date 2022/8/29 17:18
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 创建订单
     *
     * @param po
     * @return
     */
    @PostMapping("/add/order")
    @MyLog(title = "订单创建", methodType = MethodTypeEnum.INSERT)
    public R<SaleOrderDTO> createOrder(@RequestBody @Valid AddOrderPO po) {
        return RUtil.success(orderService.createOrder(po));
    }


    /**
     * 分页获取订单信息
     *
     * @param po
     * @return
     */
    @PostMapping("/query/order")
    @MyLog(title = "订单查询", methodType = MethodTypeEnum.SELECT)
    public R<Page<SaleOrderDTO>> queryOrder(@RequestBody @Valid QuerySaleOrderPO po) {
        return RUtil.success(orderService.querySaleOrder(po));
    }

    /**
     * 编辑订单信息
     *
     * @param po
     * @return
     */
    @PostMapping("/edit/order")
    public R editOrder(@RequestBody @Valid EditOrderPO po) {
        return RUtil.success(orderService.editOrder(po));
    }

    /**
     * 通过id/订单号移除订单信息
     *
     * @param po
     * @return
     */
    @PostMapping("/delete/order")
    public R deleteOrder(@RequestBody @Valid DeleteOrderPO po) {
        return RUtil.success(orderService.deleteOrder(po));
    }


    /**
     * 订单确认
     *
     * @param po
     * @return
     */
    @PostMapping("/confirm/order")
    public R confirmOrder(@RequestBody @Valid ConfirmOrderPO po) {
        return RUtil.success(orderService.confirmOrder(po));
    }

    /**
     * 订单审核
     *
     * @param po
     * @return
     */
    @PostMapping("/examine/order")
    public R examineOrder(@RequestBody @Valid ConfirmOrderPO po) {
        return RUtil.success(orderService.examine(po));
    }


    /**
     * 订单完成
     *
     * @param po
     * @return
     */
    @PostMapping("/complete/order")
    public R complete(@RequestBody @Valid ConfirmOrderPO po) {
        return RUtil.success(orderService.complete(po));
    }

    /**
     * 订单详情
     *
     * @param productInfoPO
     * @return
     */
    @PostMapping("/query/order/info")
    public R<QuerySaleOrderProductDTO> queryOrderInfo(@RequestBody @Valid SaleOrderProductInfoPO productInfoPO) {
        if (ObjectUtil.isAllEmpty(productInfoPO.getOrderId(), productInfoPO.getOrderSn())) {
            return RUtil.fail("操作数不能为空");
        }
        return RUtil.success(orderService.queryOrderInfo(productInfoPO));
    }
}
